<?php

/**
 * VidsImage used for dell with image object
 *
 * @author Mina.Gerges
 */
class MaxImage
{

    /**
     * image path
     * @var string 
     */
    var $image;

    /**
     * default path
     * @var string 
     */
    var $default;

    /**
     *
     * @param string $pimage 
     */
    public function __construct($pimage)
    {
        $this->image = $pimage;
    }

    /**
     * Return true if image exist
     * @return boolean
     */
    public function checkImage()
    {
        return is_file($this->image);
    }

    /**
     * Return image path if exist -Return default path if image doesn't exist
     * @return string 
     */
    public function getImage()
    {
        return $this->checkImage() ? $this->image : $this->default;
    }

    public function getMimeType()
    {
        return mime_content_type($this->checkImage() ? $this->image : $this->default);
    }

    public function load()
    {
        return file_get_contents($this->checkImage() ? $this->image : $this->default);
    }

    /**
     * Resize and save image with new width and height  
     * @return void
     */
    public function resizeImage($pmaxWidth, $pmaxHeight)
    {
        $_image  = new sfImage($this->image);
//        $_width = $_image->getWidth() < $pmaxWidth ? $_image->getWidth() : $pmaxWidth;
//        $_height = $_image->getHeight() <$pmaxHeight ? $_image->getHeight() :$pmaxHeight;
        $_width  = $pmaxWidth;
        $_height = $pmaxHeight;
        $_image->resize($_width, $_height);
        $_image->save();
    }

    /**
     * Resize and save image with new width and height  
     * @return void
     */
    public function resizeImageOnTheFlay($pmaxWidth, $pmaxHeight)
    {
        /*
         * check if image exist if not use the default
         */
        if (is_file($this->image))
        {
            $_image = new sfImage($this->image);
        }
        else
        {
            $_image = new sfImage($this->default);
        }
//        $_width = $_image->getWidth() < $pmaxWidth ? $_image->getWidth() : $pmaxWidth;
//        $_height = $_image->getHeight() <$pmaxHeight ? $_image->getHeight() :$pmaxHeight;
        $_width  = $pmaxWidth;
        $_height = $pmaxHeight;
        return $_image->resize($_width, $_height);
    }

}
